import cv2import matplotlib.pyplot as plt!pip install pytictocfrom pytictoc import TicToct = TicToc() #create instance of classx
t.tic()img = cv2.imread('me.jpg', 1)img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)plt.imshow(img),plt.title('COLORIMAGE')#,plt.xticks([]),plt.yticks([])plt.show()t.toc()plt.imshow(img),plt.title('COLORIMAGE'),plt.xticks([]),plt.yticks([])plt.show()x
t.tic()img2 = cv2.imread('me.jpg', 1)b,g,r = cv2.split(img2)img2 =cv2.merge((r,g,b))plt.imshow(img2),plt.title('COLORIMAGE'),plt.xticks([]),plt.yticks([])plt.show()t.toc()j = 0for filename in dir(cv2): if filename.startswith('COLOR_'): print(filename) j+=1print('Ther are ' + str(j) + 'Colorspace Conversion flags in OpenCV')import numpy as npc = cv2.cvtColor(np.uint8([[[255,0,0]]]),cv2.COLOR_BGR2HSV)print(c)x
img3 = cv2.imread('cat.jpeg', 1)img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB)plt.imshow(img3),plt.title('cat')#,plt.xticks([]),plt.yticks([])plt.show()a = np.array([40,50,50])ahsv = cv2.cvtColor(img3, cv2.COLOR_RGB2HSV)image_mask = cv2.inRange(hsv, np.array([50,10,10]), np.array([255,255,255]))output = cv2.bitwise_and(img3, img3, mask=image_mask)plt.imshow(output),plt.title('output'),plt.xticks([]),plt.yticks([])plt.show()#blue = cv2.inRange(hsv, np.array([100,50,50]), np.array([255,255,255]))brown = cv2.inRange(hsv, np.array([200,160,130]), np.array([255,255,255]))orange = cv2.inRange(hsv, np.array([10,10,174]), np.array([110,255,255]))#green = cv2.inRange(hsv, np.array([40,50,50]), np.array([255,255,255]))image_mask = cv2.add(brown,orange)output = cv2.bitwise_and(img3, img3, mask=image_mask)plt.imshow(output),plt.title('output'),plt.xticks([]),plt.yticks([])plt.show()x
img4 = cv2.imread('bird.jpg', 1)upscale = cv2.resize(img4, None, fx=1.5, fy=1.5, interpolation=cv2.INTER_CUBIC)upscale = cv2.cvtColor(upscale,cv2.COLOR_BGR2RGB)downscale = cv2.resize(img4, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)downscale = cv2.cvtColor(downscale,cv2.COLOR_BGR2RGB)plt.figure(num='Image transformations',figsize=(12,8)) #1 inch = 0.39cmplt.subplot(1,2,1)plt.title('upscale')plt.imshow(upscale)plt.subplot(1,2,2)plt.title('downscale')plt.imshow(downscale)plt.show()j = 0for filename in dir(cv2): if filename.startswith('INTER_'): print(filename) j+=1print('Ther are ' + str(j) + 'interpolation method parameters in OpenCV')x
img5 = cv2.imread('bird.jpg', 1)input = cv2.cvtColor(img5, cv2.COLOR_BGR2RGB)rows, cols, channel = input.shapeT = np.float32([[1,0,-50],[0,1,50]])output = cv2.warpAffine(input, T, (cols,rows))plt.imshow(output), plt.title('Shifted Image')plt.show()x
img6 = cv2.imread('bird.jpg', 1)input = cv2.cvtColor(img6, cv2.COLOR_BGR2RGB)rows, cols, channel = input.shapeR = cv2.getRotationMatrix2D((cols/2,rows/2),50,0.5)output = cv2.warpAffine(input,R,(cols,rows))plt.imshow(output), plt.title('Rotated and Downscaled Image')plt.show()from time import sleepimg7 = cv2.imread('bird.jpg', 1)input = cv2.cvtColor(img7, cv2.COLOR_BGR2RGB)rows, cols, channel = input.shapeangle = 0while(1): if angle ==360: angle = 0 M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1) rotated = cv2.warpAffine(input,M,(cols,rows)) plt.imshow(rotated), plt.title('Rotating Image') plt.show() angle += 1 sleep(0.2) import cv2import numpy as npfrom matplotlib import pyplot as pltimg9 = cv2.imread('rabbit.png', 1)input = cv2.cvtColor(img9, cv2.COLOR_BGR2RGB)rows, cols, channel = input.shapepoints1 = np.float32([[100,100],[300,100],[100,300]])points2 = np.float32([[200,150],[400,150],[100,300]])A = cv2.getAffineTransform(points1,points2)output = cv2.warpAffine(input,A,(cols,rows))plt.figure(figsize=(12,8))plt.subplot(121),plt.imshow(input),plt.title('Input')plt.subplot(122),plt.imshow(output),plt.title('Affine Output')plt.show()import cv2import numpy as npfrom matplotlib import pyplot as pltimg10 = cv2.imread('friend.jpg', 1)input = cv2.cvtColor(img10, cv2.COLOR_BGR2RGB)rows, cols, channel = input.shapepoints1 = np.float32([[0,0],[400,0],[0,400],[400,400]])points2 = np.float32([[0,0],[300,0],[0,300],[300,300]])P = cv2.getPerspectiveTransform(points1,points2)output = cv2.warpPerspective(input,P,(200,300))plt.figure(figsize=(12,8))plt.subplot(121),plt.imshow(input),plt.title('Input')plt.subplot(122),plt.imshow(output),plt.title('Perspective Transform')plt.show()import cv2import numpy as npfrom matplotlib import pyplot as pltimg11 = cv2.imread('friend.jpg', 1)input = cv2.cvtColor(img11, cv2.COLOR_BGR2RGB)rows, cols, channel = input.shapepoints1 = np.float32([[0,0],[400,0],[0,400],[400,400]])points2 = np.float32([[0,100],[300,0],[100,300],[300,300]])P = cv2.getPerspectiveTransform(points1,points2)output = cv2.warpPerspective(input,P,(300,300))plt.figure(figsize=(12,8))plt.subplot(121),plt.imshow(input),plt.title('Input')plt.subplot(122),plt.imshow(output),plt.title('Perspective Transform')plt.show()xxxxxxxxxxwork best on grayscale images1.cv2.threshold()2.cv2.THRESH_BINARY:work best on grayscale images 1.cv2.threshold() 2.cv2.THRESH_BINARY:
j = 0for filename in dir(cv2): if filename.startswith('THRESH'): print(filename) j+=1print('Ther are ' + str(j) + 'threshold method parameters in OpenCV')import cv2from matplotlib import pyplot as pltimg12 = cv2.imread('grayscale.jpg', 0)th = 127max_val = 255ret, o1 = cv2.threshold(img12,th,max_val,cv2.THRESH_BINARY)ret, o2 = cv2.threshold(img12,th,max_val,cv2.THRESH_BINARY_INV)ret, o3 = cv2.threshold(img12,th,max_val,cv2.THRESH_TOZERO)ret, o4 = cv2.threshold(img12,th,max_val,cv2.THRESH_TOZERO_INV)ret, o5 = cv2.threshold(img12,th,max_val,cv2.THRESH_TRUNC)titles = ['Input Image','BINARY','BINARY_INV','TOZERO','TOZERO_INV','TRUNC']output = [img12, o1, o2, o3, o4, o5]for i in range(6): plt.subplot(2,3,i+1),plt.imshow(output[i],cmap='gray'),plt.title(titles[i]),plt.xticks([]),plt.yticks([])plt.show()import cv2from matplotlib import pyplot as pltimg13 = cv2.imread('oni.png', 0)ret, output = cv2.threshold(img13,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)plt.figure(figsize=(12,8))plt.subplot(121),plt.imshow(img13,cmap='gray'),plt.title('Ibarakasen origin'),plt.xticks([]),plt.yticks([])plt.subplot(122),plt.imshow(output,cmap='gray'),plt.title('Ibarakasen threshold'),plt.xticks([]),plt.yticks([])plt.show()import cv2import matplotlib.pyplot as pltimg14 = cv2.imread('friend.jpg', 0) #直接读为灰度图像ret,th1 = cv2.threshold(img14,127,255,cv2.THRESH_BINARY)th2 = cv2.adaptiveThreshold(img14,255,cv2.ADAPTIVE_THRESH_MEAN_C,\cv2.THRESH_BINARY,11,2) #换行符号 \th3 = cv2.adaptiveThreshold(img14,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\cv2.THRESH_BINARY,11,2) #换行符号 \images = [img14,th1,th2,th3]plt.figure(figsize=(12,12))for i in range(4): plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')plt.show()